package com.szp.springbootvue.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;


import java.util.concurrent.TimeUnit;

/*
    redis操作相关类
 */
@Component
public class RedisUtil {

    private static RedisTemplate redisTemplate;

    @Autowired
    public void setRedisTemplate(RedisTemplate redisTemplate) {
        RedisUtil.redisTemplate = redisTemplate;
    }
    public static void addUserToken(String username,String token){
        //设置token
        redisTemplate.boundValueOps(username).set(token);
        //设置过期时间
        redisTemplate.expire(username,1000 * 60 * 60 * 4, TimeUnit.MILLISECONDS);
        String tokenOld = (String)redisTemplate.boundValueOps(username).get();
        System.out.println("tokenNew ---- : " + tokenOld);

        String tokenNew = (String)redisTemplate.boundValueOps(username).get();
        System.out.println("tokenNew ---- : " + tokenNew);
    }
    public static boolean isUserTokenAlive(String username){
        String tokenOld = (String)redisTemplate.boundValueOps(username).get();
        return tokenOld == null ? false : true;
    }
}
